﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PoemGame.ComputerAnswer.Shared;
using Dapper;

namespace PoemGame.ComputerAnswer.Db
{
    public class ComputerAnswerDuishi:IComputerAnswer
    {
        private readonly IDbConnection conn;
        public ComputerAnswerDuishi(IDbConnection _conn)
        {
            conn = _conn;
        }

        public string Answer(string gamecontext, string lastanswer)
        {

            var lastAnswer=conn.QueryFirstOrDefault<PoemLine>("select * from PoemLine where LineContent=@line", new { line= lastanswer });
            
            if (lastAnswer!=null)
            {

                var lines = conn.Query<PoemLine>("select * from PoemLine where PoemId=@poemId", new { poemId=lastAnswer.PoemId }).ToList(); ;
                foreach (var line in lines)
                {
                    if (line.Order == lastAnswer.Order + 1) return line.LineContent;
                }
            }
            return "";
        }
    }
}
